import time
import subprocess
import sys
# 

args = sys.argv

if len(args) > 1:
    if args[1] == 'g':
        p1 = subprocess.Popen('git add --all', stdout=subprocess.PIPE, shell=True)
        p1.communicate()
        p1.wait()
        if not p1.returncode:
            p1 = subprocess.Popen(f'git commit -m {args[-1]}', stdout=subprocess.PIPE, shell=True)
            p1.communicate()
            p1.wait()
            if not p1.returncode:
                p1 = subprocess.Popen('git push', stdout=subprocess.PIPE, shell=True)
                p1.communicate()
                p1.wait()
    elif args[1] == 'b':
        p1 = subprocess.Popen('hexo clean', stdout=subprocess.PIPE, shell=True)
        p1.communicate()
        p1.wait()
        p2 = subprocess.Popen('hexo g', stdout=subprocess.PIPE, shell=True)
        p2.communicate()
        p2.wait()
        p3 = subprocess.Popen('hexo d', stdout=subprocess.PIPE, shell=True)
        p3.communicate()
        p3.wait()
        # try:
        #     option = webdriver.ChromeOptions()
        #     driver = webdriver.Chrome(service=Service('./chromedriver.exe'), options=option)
        # except Exception as e:
        #     print(e)
        #     print("启动谷歌失败, 现在启动edge")
        #     option = webdriver.EdgeOptions()
        #     option.add_argument('--headless')
        #     driver = webdriver.Edge(service=Service('./msedgedriver.exe'), options=option)
        # try:
        #     driver.get("https://gitee.com/login")
        #     time.sleep(3)
        #     driver.find_element(By.ID, "user_login").send_keys('13196942325')
        #     driver.find_element(By.ID, 'user_password').send_keys('wk13196942325')
        #     driver.find_element(By.XPATH, '//*[@id="new_user"]/div/div/div/div[4]/input').click()
        #     time.sleep(5)
        #     driver.get("https://gitee.com/walker_kwu/my-blog/pages")
        #     time.sleep(5)
        #     driver.find_element(By.CSS_SELECTOR, '.button.orange.redeploy-button.ui.update_deploy').click()
        #     time.sleep(5)
        #     driver.switch_to.alert.accept()
        #     time.sleep(3)
        #     while True:
        #         try:
        #             driver.find_element(By.ID, "pages_deploying")
        #             print("正在更新...")
        #         except:
        #             print("更新结束")
        #             break
        #         time.sleep(1)
        # finally:
        #     driver.quit()
    elif args[1] == 'a':
        p1 = subprocess.Popen('python update.py g', stdout=subprocess.PIPE, shell=True)
        p1.communicate()
        p1.wait()
        p1 = subprocess.Popen('python update.py b', stdout=subprocess.PIPE, shell=True)
        p1.communicate()
        p1.wait()